﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class BolBLL
    {
        private static readonly IBolDA BolDA = DataAccess.CreateBolDA();
        private static readonly IBolDetailDA BolDetailDA = DataAccess.CreateBolDetailDA();
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IDeliveryOrderDA DeliveryOrderDA = DataAccess.CreateDeliveryOrderDA();
        private PubParasInfo pubParasInfo;
        private static readonly ITransportEquipmentDA TransportEquipmentDA = DataAccess.CreateTransportEquipmentDA();

        public BolBLL()
        {
            this.pubParasInfo = null;
        }

        public BolBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public BolBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteBol(List<string> bolIDs)
        {
            return "";
        }

        public DataSet GetAllInfo()
        {
            return BolDA.GetAllBol(this.dataBaseID[0]);
        }

        public BolInfo GetInfoByID(string bolID)
        {
            return BolDA.GetBolByID(bolID, this.dataBaseID[0]);
        }

        public DataSet GetInfoByQueryEntity(BolQueryEntity bolQuery)
        {
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(bolQuery.BolID))
            {
                sqlWhere.Add(" BOL_ID " + bolQuery.BolID);
            }
            return BolDA.GetBolByQueryList(sqlWhere, bolQuery, this.dataBaseID[0]);
        }

        public string InsertBol(BolInfo bolInfo)
        {
            return "";
        }

        public string UpdateBol(BolInfo bolInfo)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string result = "";
            try
            {
                if (BolDA.UpdateBol(dataBase, tran, bolInfo, this.dataBaseID) > 0)
                {
                    result = bolInfo.BolID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }
    }
}

